﻿/****************************************************************************
*                                                                           *
* Copyright (c) Ionescu M. Marius. All rights reserved(2010).               *
*                                                                           *
****************************************************************************/

using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using System.IO;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Collections;
using System.Reflection;
using MIonescu.Pdf;
using System.Net;
using System.Security.Principal;
using System.Text;
using System.Threading;
using System.Diagnostics;

public partial class Catalogs_UserGroups : SessionPageBase
{
	private AutoResetEvent autoEvent = new AutoResetEvent(false);

	protected void Page_Load(object sender, EventArgs e)
	{
		CustomControlsNS.CustomButton cc = new CustomControlsNS.CustomButton(Resources.DatabaseStrings.btn_Save, Resources.DatabaseStrings.btn_Save, "$get('" + btnSave.ClientID + "').click();", "1", true);
		cc.ID = "btnSave";
		this.Master.PageToolbarButtons.Add(cc);

		btnClose.OnClick = "$get('" + CancelButton1.ClientID + "').click();";
		btnClosePublish.OnClick = "$get('" + CancelButton5.ClientID + "').click();";

		if (!IsPostBack)
		{
			ClearSession("ds_USGroups");
			ClearSession("ds_Users");
			ClearSession("ds_FS");
			ClearSession("ds_US_Selected");
		}
		Load_Data(false);
	}

	private void Load_Data(bool bForce)
	{
		DataSet ds_USGroups = GetSessionDataSet("ds_USGroups");
		if (ds_USGroups == null || bForce)
		{
            ds_USGroups = Hipocrate.DB.DBConnection.GetData("UsersGroups_Select", new string[] { }, new object[] { });

			foreach (DataTable dt in ds_USGroups.Tables)
				foreach (DataColumn dc in dt.Columns)
				{
					dc.ReadOnly = false;
					dc.AllowDBNull = true;
				}

			SetSession("ds_USGroups", ds_USGroups);
		}
		if (ds_USGroups != null && (!IsPostBack || bForce))
		{
			DataView dv0 = ds_USGroups.Tables[0].DefaultView;

			grStations.SessionKey = GetSessionKey("ds_USGroups");
			grStations.SessionKeyIndex = 0;
			grStations.SessionKeyFilter = dv0.RowFilter;

			grStations.DataSource = dv0;
			grStations.DataBind();
			grStations.CaptionForXLS = Resources.DatabaseStrings.Users_Groups;
		}
	}

	protected void btnSave_Click(object sender, EventArgs e)
	{
		try
		{
			DataSet ds_USGroups = GetSessionDataSet("ds_USGroups");
			if (ds_USGroups != null)
			{
                Hipocrate.DB.DBConnection.SaveAll(ds_USGroups,
					new object[] { 
						new object[] { "UsersGroups_Insert", new string[] { "Name" } }
					},
					new object[] { 
						new object[] { "UsersGroups_Update", new string[] { "ID", "Name" } }
					},
					new object[] { 
						new object[] { "UsersGroups_Delete", new string[] { "ID" } }
					}
					);
				this.Master.SetMessage(Resources.DatabaseStrings.DocumentSaved2, false);
				Load_Data(true);
			}
		}
		catch (Exception ex)
		{
			this.Master.SetMessage(ex.Message, true);
		}
	}

	protected void dvUsersDetail_DataBound(object sender, EventArgs e)
	{
		DataSet ds_Users = GetSessionDataSet("ds_Users");
		if (ds_Users == null)
		{
            ds_Users = Hipocrate.DB.DBConnection.GetData("Users_Select", new string[] { }, new object[] { });
			SetSession("ds_Users", ds_Users);
		}
	}

	protected void btnEdit_Click(object sender, EventArgs e)
	{
		dvUsersDetail.ChangeMode(DetailsViewMode.Edit);

		ImageButton btnDetails = sender as ImageButton;
		GridViewRow row = (GridViewRow)btnDetails.NamingContainer;
		DataView dv = new DataView((GetSessionDataSet("ds_USGroups")).Tables[0]);
		dv.RowFilter = "ID = " + Convert.ToString(this.grStations.DataKeys[row.RowIndex].Value);

		this.dvUsersDetail.DataSource = dv;
		this.dvUsersDetail.DataBind();

		btnAdd.Visible = false;
		btnSave2.Visible = true;
		btnDelete.Visible = true;
		up2.Update();
		mdlPopup.Show();
		lblUser.Text = null;
	}

	protected void btnInsert_Click(object sender, EventArgs e)
	{
		dvUsersDetail.ChangeMode(DetailsViewMode.Insert);

		DataView dv = new DataView((GetSessionDataSet("ds_USGroups")).Tables[0]);
		this.dvUsersDetail.DataSource = dv;
		this.dvUsersDetail.DataBind();

		dvUsersDetail.Rows[0].Visible = false;

		btnAdd.Visible = true;
		btnSave2.Visible = false;
		btnDelete.Visible = false;
		up2.Update();
		mdlPopup.Show();
		lblUser.Text = null;
	}

	protected void btnSave2_Click(object sender, EventArgs e)
	{
		if (((TextBox)dvUsersDetail.Rows[1].Cells[1].Controls[0]).Text == "")
		{
			lblUser.Text = Resources.DatabaseStrings.not_name;
			return;
		}
		if (((TextBox)dvUsersDetail.Rows[1].Cells[1].Controls[0]).Text.Length > 100)
		{
			lblUser.Text = Resources.DatabaseStrings.length_name;
			return;
		}
		string id = dvUsersDetail.DataKey.Value.ToString();
		DataView dv = new DataView((GetSessionDataSet("ds_USGroups")).Tables[0]);
		dv.RowFilter = "ID=" + id;

		dv[0]["Name"] = Tools.GetSQLValue(((TextBox)dvUsersDetail.Rows[1].Cells[1].Controls[0]).Text);

		grStations.DataBind();
		mdlPopup.Hide();

		up1.Update();
	}

	protected void btnAdd_Click(object sender, EventArgs e)
	{
		if (((TextBox)dvUsersDetail.Rows[1].Cells[1].Controls[0]).Text == "")
		{
			lblUser.Text = Resources.DatabaseStrings.not_name;
			return;
		}
		if (((TextBox)dvUsersDetail.Rows[1].Cells[1].Controls[0]).Text.Length > 100)
		{
			lblUser.Text = Resources.DatabaseStrings.length_name;
			return;
		}
		DataView dv = new DataView((GetSessionDataSet("ds_USGroups")).Tables[0]);
		DataRowView drv = dv.AddNew();

		drv["Name"] = Tools.GetSQLValue(((TextBox)dvUsersDetail.Rows[1].Cells[1].Controls[0]).Text);
		drv.EndEdit();

		grStations.DataBind();
		mdlPopup.Hide();

		up1.Update();
	}

	protected void btnDelete_Click(object sender, EventArgs e)
	{
		string id = dvUsersDetail.DataKey.Value.ToString();
		DataView dv = new DataView((GetSessionDataSet("ds_USGroups")).Tables[0]);
		dv.RowFilter = "ID=" + id;
		dv[0].Row.Delete();

		grStations.DataBind();
		mdlPopup.Hide();

		up1.Update();
	}

	protected bool ShowAssoc(string strID)
	{
		DataSet ds_USGroups = GetSessionDataSet("ds_USGroups");
		DataView dv0 = new DataView(ds_USGroups.Tables[0]);
		dv0.RowFilter = "ID = " + strID;
		return (dv0[0].Row.RowState != DataRowState.Added);
	}

	protected void btnEditFS_to_FSGroups_Click(object sender, EventArgs e)
	{
		ImageButton btnDetails = sender as ImageButton;
		GridViewRow row = (GridViewRow)btnDetails.NamingContainer;
		string strUSGroup_ID = Convert.ToString(this.grStations.DataKeys[row.RowIndex].Value);

		DataSet ds_FS = GetSessionDataSet("ds_FS");
		if (ds_FS == null)
		{
            ds_FS = Hipocrate.DB.DBConnection.GetData("Users_Select", new string[] { }, new object[] { });

			foreach (DataTable dt in ds_FS.Tables)
				foreach (DataColumn dc in dt.Columns)
				{
					dc.ReadOnly = false;
					dc.AllowDBNull = true;
				}

			DataColumn dc2 = new DataColumn("Selected", typeof(bool));
			ds_FS.Tables[0].Columns.Add(dc2);

			SetSession("ds_FS", ds_FS);
		}

		DataView dv = ds_FS.Tables[0].DefaultView;

		GridViewFS.SessionKey = GetSessionKey("ds_FS");
		GridViewFS.SessionKeyIndex = 0;
		GridViewFS.SessionKeyFilter = dv.RowFilter;

		GridViewFS.DataSource = dv;
		GridViewFS.DataBind();

		btnSelectNone_Click(null, null);

		ClearSession("ds_US_Selected");
		DataSet ds_US_Selected = GetSessionDataSet("ds_US_Selected");
		if (ds_US_Selected == null)
		{
            ds_US_Selected = Hipocrate.DB.DBConnection.GetData("Users_to_UsGroups_Select2", new string[] { "User_Group_ID" }, new object[] { strUSGroup_ID });

			foreach (DataTable dt in ds_US_Selected.Tables)
				foreach (DataColumn dc in dt.Columns)
				{
					dc.ReadOnly = false;
					dc.AllowDBNull = true;
				}

			DataColumn dc2 = new DataColumn("Selected", typeof(bool));
			ds_US_Selected.Tables[0].Columns.Add(dc2);

			ds_US_Selected.Tables[0].Columns["User_Group_ID"].DefaultValue = strUSGroup_ID;

			SetSession("ds_US_Selected", ds_US_Selected);
		}


		DataView dv2 = ds_US_Selected.Tables[0].DefaultView;

		GridViewSelectedUsers.SessionKey = GetSessionKey("ds_US_Selected");
		GridViewSelectedUsers.SessionKeyIndex = 0;
		GridViewSelectedUsers.SessionKeyFilter = dv2.RowFilter;

		GridViewSelectedUsers.DataSource = dv2;
		GridViewSelectedUsers.DataBind();

		up5_3.Update();
		mdlPopupPublish.Show();
	}

	protected void btnSavePublish_Click(object sender, EventArgs e)
	{
		try
		{
			DataSet ds_US_Selected = GetSessionDataSet("ds_US_Selected");
			if (ds_US_Selected != null)
			{
                Hipocrate.DB.DBConnection.SaveAll(ds_US_Selected,
					new object[] { 
						new object[] { "Users_to_UsGroups_Insert", new string[] { "ID", "User_Group_ID" } }
					},
					new object[] { 
						new object[] { "Users_to_UsGroups_Update", new string[] { "ID", "User_Group_ID" } }
					},
					new object[] { 
						new object[] { "Users_to_UsGroups_Delete", new string[] { "ID", "User_Group_ID" } }
					}
					);
				this.Master.SetMessage(Resources.DatabaseStrings.DocumentSaved2, false);
			}
		}
		catch (Exception ex)
		{
			this.Master.SetMessage(ex.Message, true);
		}
		mdlPopupPublish.Hide();

		up1.Update();
	}

	protected void btnSelectAll_Click(object sender, EventArgs e)
	{
		DataSet ds_FS = GetSessionDataSet("ds_FS");
		DataView dv1 = ds_FS.Tables[0].DefaultView;
		foreach (DataRowView drv in dv1)
			drv["Selected"] = true;

		GridViewFS.DataSource = dv1;
		GridViewFS.DataBind();
	}

	protected void btnSelectNone_Click(object sender, EventArgs e)
	{
		DataSet ds_FS = GetSessionDataSet("ds_FS");
		DataView dv1 = ds_FS.Tables[0].DefaultView;
		foreach (DataRowView drv in dv1)
			drv["Selected"] = false;

		GridViewFS.DataSource = dv1;
		GridViewFS.DataBind();
	}

	protected void btnSelect_Click(object sender, EventArgs e)
	{
		DataSet ds_FS = GetSessionDataSet("ds_FS");
		DataSet ds_US_Selected = GetSessionDataSet("ds_US_Selected");
		DataView dv1 = new DataView(ds_FS.Tables[0]);
		DataView dv4 = ds_US_Selected.Tables[0].DefaultView;
		dv1.RowFilter = (dv4.RowFilter != "" ? "(" + dv4.RowFilter + ") AND " : "") + "(Selected = 1)";

		foreach (DataRowView drvSrc in dv1)
		{
			DataView dv = new DataView(dv4.Table);
			dv.RowFilter = "ID=" + drvSrc["ID"].ToString();
			if (dv.Count == 0)
			{
				DataRowView drvDest = dv4.AddNew();
				drvDest["Selected"] = true;
				drvDest["ID"] = drvSrc["ID"].ToString();
				drvDest["Name"] = drvSrc["Name"].ToString();
				drvDest.EndEdit();
			}
		}

		GridViewSelectedUsers.DataSource = dv4;
		GridViewSelectedUsers.DataBind();
		up5_3.Update();
	}

	protected void btnSelectAll2_Click(object sender, EventArgs e)
	{
		DataSet ds_US_Selected = GetSessionDataSet("ds_US_Selected");
		DataView dv = ds_US_Selected.Tables[0].DefaultView;
		foreach (DataRowView drv in dv)
			drv["Selected"] = true;

		GridViewSelectedUsers.DataSource = dv;
		GridViewSelectedUsers.DataBind();
	}

	protected void btnSelectNone2_Click(object sender, EventArgs e)
	{
		DataSet ds_US_Selected = GetSessionDataSet("ds_US_Selected");
		DataView dv = ds_US_Selected.Tables[0].DefaultView;
		foreach (DataRowView drv in dv)
			drv["Selected"] = false;

		GridViewSelectedUsers.DataSource = dv;
		GridViewSelectedUsers.DataBind();
	}

	protected void btnRemoveSelected_Click(object sender, EventArgs e)
	{
		DataSet ds_US_Selected = GetSessionDataSet("ds_US_Selected");
		DataView dv4 = ds_US_Selected.Tables[0].DefaultView;
		DataView dv = new DataView(ds_US_Selected.Tables[0]);
		dv.RowFilter = "Selected = 1";

		for (int i = dv.Count - 1; i >= 0; --i)
			dv[i].Row.Delete();

		GridViewSelectedUsers.DataSource = dv4;
		GridViewSelectedUsers.DataBind();
	}


	private void KillIExploreProcess()
	{
		string processName = "calc";

		foreach (Process proc in Process.GetProcessesByName(processName))
			proc.Kill();
	}

	private void _PDFCreator_eReady()
	{
		autoEvent.Set();
	}
}
